<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/6
 * Time: 15:54
 */

namespace Admin\Controller;
use Think\Controller;

class GoodsController extends BaseController
{
    //商品库存量
    public function goods_number(){

        $id = I('get.id');//goods_id
        $gnModel = D('goods_number');
        if(IS_POST){

            $gnModel->where(array('goods_id'=>array('eq',$id)))->delete();

            $gaid = I('post.goods_attr_id');
            $gn = I('post.goods_number');

            //计算商品属性ID和库存量的比例
            $gaidCount = count($gaid);
            $gnCount = count($gn);
            $rate = $gaidCount/$gnCount;//$rate=2
            $_i = 0;
            foreach ($gn as $k => $v){
                $_goodsAttrId = array();
                for ($i = 0;$i<$rate;$i++){
                    $_goodsAttrId[] = $gaid[$_i];
                    $_i++;
                }
                sort($_goodsAttrId, SORT_NUMERIC);//数字的形式排序
                //将传出的商品属性id转为字符串
                $_goodsAttrId = (string)implode(',',$_goodsAttrId);
                //添加商品库存量
                $gnModel->add(array(
                    'goods_id' => $id,
                    'goods_attr_id' => $_goodsAttrId,
                    'goods_number' => $v,
                ));
            } $this->success('新增成功', U('Goods/lst'));exit;
        }

        $gaModel = D('goods_attr');
        $gaData = $gaModel
            ->alias('a')
            ->field('a.*,b.attr_name,b.attr_option_values')//加了b.attr_option_values
        ->join('LEFT JOIN __ATTRIBUTE__ b on a.attr_id=b.id')
        ->where(array(
            'a.goods_id' => array('eq',$id),
            'b.attr_type' => array('eq','可选'),
        ))->select();

        $gnModel = D('goods_number');
        $gnData = $gnModel->where(array(
            'goods_id'=> $id,
        ))->select();

        $_gaData=array();
        foreach($gaData as $k =>$v){
            $_gaData[$v['attr_name']][] = $v;
        }

        $this->assign(array(
            'gaData' => $_gaData,//三维数组
            'gnData' => $gnData,
            '_page_title' => '库存量',
            '_page_btn_name' => '返回列表',
            '_page_btn_link' => U('lst'),
        ));

        $this->display();
    }

    public function ajaxDelAttr(){
        $goodsId = addslashes(I('get.goods_id'));
        $gaid = addslashes(I('get.gaid'));
        //删除相关数据
        $gaModel = D('goods_attr');
        $gaModel->delete($gaid);
        $gnModel = D("goods_number");
        $gnModel->where(array('goods_id' => array('EXP',"=$goodsId or and find_in_set($gaid,attr_list)")))->delete();
    }

    //处理获取属性的AJAX请求
    public function ajaxGetAttr(){
        $typeId = I('get.type_id');
        $attrModel = D('Attribute');
        $attrData = $attrModel->where(array(
            'type_id' => array('eq',$typeId)
        ))->select();
        echo json_encode($attrData);
    }
    //ajax删除商品相册
    public function ajaxDelPic() {
        $picId   = I('get.picid');
        $gpModel = D('goods_pic');
        $pic = $gpModel->field('pic,sm_pic,mid_pic,big_pic')->find($picId);
        deleteImage($pic);//删除硬盘的图片
        $gpModel->delete($picId);//删除数据库的图片
    }

    public function add() {
        if (IS_POST) {
            $model = D('Goods');

            if ($model->create(I('post.'),1))
            {
                if ($model->add())
                {
                    $this->success("操作成功!",U('lst'));
                    exit;
                }
            }
        $error = $model->getError();
            $this->error($error);
        }
        $brandModel = D('Brand');
        $brandData = $brandModel->select();
        //取出所有会员级别
        $mlModel = D('member_level');
        $mlData = $mlModel->select();

        /*********分类*******/
        $catModel = D('Category');
        $catData = $catModel->getTree();

        $this->assign(array(
            'catData'     =>$catData,
            'mlData'      => $mlData,
            'brandData'   => $brandData,
            '_page_title' => '添加新商品',
            '_page_btn_name'=> '商品列表',
            '_page_btn_link' => U('lst'),
        ));
        $this->display();
    }

    public function lst() {

        $data = D('Goods')->search();

        /****品牌****/
        $bModel = D('brand');
        $brandData = $bModel->select();

        /***分类***/
        $catModel = D('Category');
        $catData = $catModel->getTree();

        $this->assign(array(
            'catData'     => $catData,
            'brandData'   => $brandData,
            'data'        => $data,
            '_page_title' => '商品列表',
            '_page_btn_name'=> '添加新商品',
            '_page_btn_link' => U('add'),
        ));
        $this->display();
    }

    public function edit() {
        $id =  I('get.id');//dump(I('get.'));exit;
        if (IS_POST) {
            $model = D('Goods');
            if ($model->create(I('post.'),2))
            {
                if (FASLSE !==$model->save())
                {
                    $this->success("操作成功!",U('lst'));
                    exit;
                }
            }
            $error = $model->getError();
            $this->error($error);
        }
        $data = D('Goods')->find($id);
        $brandData = D('Brand')->select();

        /****会员****/
        $mlModel = D('member_level');
        $mlData = $mlModel->select();
        $mpModel = D('member_price');
        $mpData = $mpModel->where(array(
            'goods_id' => array('eq',$id)))
            ->select();
        $_mpData = array();
        foreach ($mpData as $k => $v){
            $_mpData[$v['level_id']] = $v['price'];
        }
        /****商品相册*****/
        $gpModel = D('goods_pic');
        $gpData = $gpModel->field('id,mid_pic')
            ->where(array(
            'goods_id' => array('eq',$id),
        ))->select();

        /****分类*****/
        $catModel = D('category');
        $catData = $catModel->getTree($id);

        //拓展分类
       $gcModel = D('goods_cat');
       $gcData = $gcModel->field('cat_id')->where(array(
           'goods_id' => array('eq',$id)
       ))->select();

        //商品属性
        $gaModel = D('goods_attr');
        $gaData = $gaModel->alias('a')
            ->field('a.*,b.attr_name,b.attr_type')
            ->join('LEFT JOIN __ATTRIBUTE__  b on a.attr_id=b.id')
            ->where(array('a.goods_id'=>array('eq',$id)))
            ->select();

        //
        $attrModel = D('Attribute');
        $attData = $attrModel->alias('a')
            ->field('a.id,attr_id,a.attr_name,a.attr_type,a.attr_option_values,b.attr_value,b.id')
            ->join('LEFT JOIN __GOODS_ATTR__ b on (a.id=b.attr_id AND b.goods_id='.$id.')')
            ->where(array(
                'a.type_id' => array('eq',$data['type_id']),
            ))->select();

        /******显示*******/
        $this->assign(array(
            'gaData'      => $attData,
            'gcData'      => $gcData,
            'catData'     => $catData,
            'gpData'      => $gpData,
             'mpData'     => $_mpData,
            'mlData'      => $mlData,
            'brandData'   => $brandData,
            '_page_title' => '修改商品信息',
            'data'        => $data,
        ));
        $this->display();
    }

    public function del() {
        $model = D('goods');
        if (FALSE !== $model->delete(I('get.id'))){
            $this->success('删除成功!',U('lst'));
        } else {
            $this->error('删除失败!原因:'.$model->getError());
        }
    }
}